package com.lzs.Service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lzs.DAO.RoleDAO;
import com.lzs.PO.Page;
import com.lzs.PO.Role;
import com.lzs.Service.RoleService;

@Service("RoleService")
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleDAO roleDAO;

	public Page<Role> findRolePagelist(String keyword, Integer pageNum, Integer pageSize) {

		List<Role> rolelist = this.roleDAO.findRoleList(keyword);
		int totalRecord = rolelist.size();

		Page page = new Page(pageNum, pageSize, totalRecord);

		int startIndex = page.getStartIndex();

		page.setList(roleDAO.findRolePagelist(keyword, startIndex, pageSize));

//		List<Role> rolelist = this.roleDAO.findRoleList(keyword);
//
//		int totalRecord = rolelist.size();
//
//		Page page = new Page(pageNum, pageSize, totalRecord);
//
//		int startIndex = page.getStartIndex();
//        
//		List<Integer> roleids = new ArrayList<Integer>();
//		
//		List<String> menuids = new ArrayList<String>();
//		
//		Map<String,Object> map = new HashMap<String,Object>();
//		
//		List<Role>roles = roleDAO.findRolePagelist(startIndex, pageSize);
//		
//		for(int i=0;i<=roles.size();i++) {
//			roleids.add(roles.get(i).getRoleid());
//			menuids.add(roles.get(i).getMenus().get(i).getMenuid());
//		}
//		map.put("roleids", roleids);
//		
//		map.put("menuids",menuids);
//		
//		page.setList(this.roleDAO.selectMenuById(map));
//		
//		page.setList(this.roleDAO.selectRoleById(map));

		return page;
	}

	public List<Role> findRoleList(String keyword) {
		List<Role> roles = this.roleDAO.findRoleList(keyword);
		return roles;
	}

	public boolean checkrole(Integer roleid) {
		long count = this.roleDAO.countByRoleid(roleid);
		return count == 0;
	}

	@Override
	public void addRoleWithMenuids(Map<String, Object> map) {
		this.roleDAO.addRoleWithMenuids(map);

	}

	@Override
	public void addRole(Role role) {
		this.roleDAO.addRole(role);

	}

	@Override
	public Role selectRoleMenuById(Integer roleid) {
		Role role = this.roleDAO.selectRoleMenuById(roleid);
		return role;

	}

	@Override
	public void updateRole(Role role) {
		this.roleDAO.updateRole(role);

	}

	@Override
	public void updateRoleMenu(Map<String, Object> map) {
		this.roleDAO.deleteRoleMenuById(map);
		this.roleDAO.addRoleWithMenuids(map);

	}

	@Override
	public void deleteRoleMenuById(Map<String, Object> map) {
		this.roleDAO.deleteRoleMenuById(map);
		
	}

	@Override
	public void deleteRoleById(Map<String, Object> map) {
		this.roleDAO.deleteRoleById(map);
		
	}

	@Override
	public List<Role> findRoles() {
		List<Role> roles = this.roleDAO.findRoles();
		return roles;
	}


}
